#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
int a[N] , p[N];
unordered_map<int,int> mp;
vector<int> v1[N];
int find(int x)
{
  if(p[x] != x) p[x] = find(p[x]);
  return p[x];
}

void marge(int a, int b)
{
  int x = find(a), y = find(b);
  if(x != y) p[y] = x;
}
int main()
{
  int n;
  cin >>  n;
  for(int i = 1; i <= n; i++) cin >> a[i] , p[i] = i , mp[a[i]] = i;
  int m; cin >>  m;
  for(int i = 1; i <= m; i++)
  {
    int b , c;
    cin  >> b >> c;
    //if(mp.count(b) && mp.count(c));
    marge(mp[b] , mp[c]);
  }

  for(int i = 1; i <= n; i++) v1[find(i)].push_back(a[i]);
  for(int i = 1; i <= n; i++) sort(v1[i].begin() , v1[i].end());
  for(int i = 1; i <= n; i++)
  {
    cout << v1[find(i)].back() << " ";
    v1[find(i)].pop_back();
  }

  return 0;
}
